<?php
//购物车
class Cart{
	private $totalCount;			//总产品数
	private $totalCost ;			//产品总价格
	private $totalDiscount;			//总折扣
	private $totalPay ;				//最后支付价格
	private $itemCart;				//购物车
	
	//构造函数
	public function __construct(){
		//session_start();

		$this->itemCart = $_SESSION['my_cart'];
		$this->upTotal();
		  

	}

	//加入商品
	public function addItem($item_id,$item_name,$item_price,$item_cnt,$item_option='',$item_surcharge='',$item_expressfee=0,$item_discount=0){
		$check_id = 0;
		$this->itemCart = $_SESSION['my_cart'];
		$item_key = md5($item_id.$item_option);
		if($this->itemCart)
		{
			foreach($this->itemCart as $k => $v)
			{
				if($k > $check_id )
				{
					$check_id = $k;
				}
				if($item_id == $this->itemCart[$k]['item_id'] && $item_key==$this->itemCart[$k]['item_key'])				//已存在商品,且属性相同,则更新数量
				{
					//$update_ary[] = $k.'-'.($item_cnt+$this->itemCart[$k]['item_cnt']);
					$this->updateItem($k,$item_cnt,$item_option,$item_surcharge,$item_expressfee);		
					$update_type = 'Y';
				}
			}
		}

		$insert_id = $check_id +1;											//新序号id

		if($update_type =='Y')												//已存在商品,update
		{
			//$this->updateItem($key,$item_cnt,$item_option,$item_surcharge,$item_expressfee);
			//$this->updateItem($update_ary);
			$update_type = '';
		}
		else															//不存在商品,insert
		{
			$this->itemCart[$insert_id]['item_id']=$item_id;				//商品id
			$this->itemCart[$insert_id]['item_name']=$item_name;			//商品名称
			$this->itemCart[$insert_id]['item_option']=$item_option;			//商品属性
			$this->itemCart[$insert_id]['item_price']=$item_price;			//商品价格
			$this->itemCart[$insert_id]['item_cnt']=$item_cnt;				//商品数量
			$this->itemCart[$insert_id]['item_key']=$item_key;						//商品属性md5
			$this->itemCart[$insert_id]['item_surcharge']=$item_surcharge;			//额外服务费
			$this->itemCart[$insert_id]['item_expressfee']=$item_expressfee;			//快递费
		
			//$this->itemCart[$insert_id]['item_discount']=$item_discount;	//商品折扣
			$this->upTotal();

		}

	}
	
	//更新数量,数组array('1-2'),'序号-数量';
/*	public function updateItem($update_ary){
		
		if($update_ary)
		{
			foreach($update_ary as $key => $val)
			{
				$this->itemCart = $_SESSION['my_cart'];
				$data = explode("-",$val);
				if($data[1] == '0')								//商品数为0就删除商品
				{
					$this->delItem($data[0]);

				}
				else
				{
					$this->itemCart[$data[0]]['item_cnt'] = $data[1];
					$this->upTotal();

				}
				
			}
		}
	}*/
	
	//更新购物车的单个商品	
	public function updateItem($key,$item_cnt,$item_option,$item_surcharge,$item_expressfee){
		$this->itemCart = $_SESSION['my_cart'];		
		$this->itemCart[$key]['item_cnt'] = $item_cnt;
		$this->itemCart[$key]['item_option'] = $item_option;
		$this->itemCart[$key]['item_surcharge'] = $item_surcharge;
		$this->itemCart[$key]['item_expressfee'] = $item_expressfee;
		$this->upTotal();		
			
	}
	
	//更新购物车,生成新的二维数组,输入的是表单传来的, 不用ajax提交表单时
	public function updateCart($item_id='',$item_name='',$item_price='',$item_cnt='',$item_option='',$item_package='',$item_discount=0){	
		if($item_id)
		{
			foreach($item_id as $kk=>$vv)
			{
				$new_cart[$kk]['item_id'] = intval($item_id[$kk]);
				$new_cart[$kk]['item_name'] = $item_name[$kk];
				$new_cart[$kk]['item_option'] = $item_option[$kk];
				$new_cart[$kk]['item_price'] = $item_price[$kk];
				$new_cart[$kk]['item_cnt'] = intval($item_cnt[$kk]);
				$new_cart[$kk]['item_key'] = md5($item_option[$kk]);
				$new_cart[$kk]['item_surcharge'] = $item_surcharge[$kk];
				//$new_cart[$kk]['item_discount'] = intval($item_discount[$kk]);
			}
			
			$this->itemCart = $new_cart;
			$this->upTotal();
		}
		
		return $this->itemCart;
	}
	
	//删除商品,指定KEY
	public function delItem($key){
		$key_ary = explode(',',$key);
		$this->itemCart = $_SESSION['my_cart'];
		foreach($key_ary as $k=>$v){
			foreach($this->itemCart as $k2=>$v2){
				if($v == $v2['item_key'] ){
					unset($this->itemCart[$k2]);
					break;
				}
			}
			
		}
		$this->upTotal();
	}
	
/*	public function delItem($del_id){
		$this->itemCart = $_SESSION['my_cart'];
		unset($this->itemCart[$del_id]);
		$this->upTotal();
	}*/

	//所有操作后更新,计算总商品数,总金额
	public function upTotal(){	
		if(empty($this->itemCart))
		{
			$this->totalCount = 0;
			$this->totalCost = 0;
			$this->totalDiscount = 0;
			$this->totalPay = 0;
			$_SESSION['my_cart'] ='';
		}
		else
		{		
			$count = 0;
			foreach($this->itemCart as $key => $value)
			{
				$count++;
				$this->itemCart[$key]['item_total'] = $this->itemCart[$key]['item_price'] * $this->itemCart[$key]['item_cnt'];
				$this->itemCart[$key]['item_discount_total'] = $this->itemCart[$key]['item_total'] * $this->itemCart[$key]['item_discount'] * 0.01;
				$total_price = $total_price + $this->itemCart[$key]['item_total'];
				$total_discount = $total_discount + $this->itemCart[$key]['item_discount_total'];
				$this->itemCart[$key]['item_pay_total'] = $this->itemCart[$key]['item_total']-$this->itemCart[$key]['item_discount_total'] ;
				$total_item_pay = round($total_item_pay + $this->itemCart[$key]['item_pay_total'],2) ;
				$total_cnt = $total_cnt + $this->itemCart[$key]['item_cnt'];
				
			}
		//$this->totalCount = $total_cnt;
		$this->totalCount = $count;
		$this->totalCost = round($total_price,2);
		$this->totalDiscount = $total_discount;
		$this->totalPay = round(($total_price - $total_discount),2);
			
		}
		$_SESSION['my_cart'] = $this->itemCart;
		//dump($_SESSION['my_cart']);
		//print_r($_SESSION['my_cart']);
		unset($this->itemCart);
	}

	//产品列表,格式id-num|id2-num2
	public function getList()
	{
		$cart = $_SESSION['my_cart'];
		if($cart){
			foreach($cart as $kk=>$vv)
			{
				$cart_ary = $vv['item_id'].'-'.$vv['item_cnt'];
				
				$cart_list .= $cart_list ?  '|'.$cart_ary : $cart_ary;	
			}
		}
		return $cart_list;
	}


	//取得购物车二维数组,不重组保持序号
	public function getCart(){
		$cart = $_SESSION['my_cart'];
		/*if($cart)
		{
			$ref = 0;
			foreach($cart as $key => $val)
			{
				$new_cart[$ref] = $cart[$key];
				$ref ++;
			}
		}
		
		$_SESSION['my_cart'] = $new_cart ;
		unset($this->itemCart);
		return $new_cart;*/
		return $cart;
	}

	//取得产品总金额
	public function getTotal()
	{
		return $this->totalCost;
	}

	//取得总商品总数
	public function getCount()
	{
		return $this->totalCount;
	}

	//取得总商品折扣总价
	public function getDiscount()
	{
		return $this->totalDiscount;
	}
	
	//取得最终需支付
	public function getPay()
	{
		return $this->totalPay;
	}

	//删除购物车
	public function delCart()
	{
		$_SESSION['my_cart'] ='';
		$this->totalCount = 0;
		$this->totalCost = 0;
		$this->totalDiscount = 0;
		$this->totalPay = 0 ;
		$this->itemCart = array();
	}
	
	
	public function save_item($data){
		$array = array();
		$item = array();
		$item_count = 0;
		$item_total = 0;
		$id = $data['item_id'];
		$name = $data['item_name'];
		$option = $data['item_option'];
		$price = $data['item_price'];
		$count = $data['item_count'];
		$total = $data['item_total'];
		$surcharge = $data['item_surcharge'];
		if(is_array($name)){
			foreach($name as $k=>$v){
				if($v){
					$item['id'] = trim($id[$k]);
					$item['name'] = trim($name[$k]);
					$item['option'] = trim($option[$k]);
					$item['price'] = trim($price[$k]);
					$item['count'] = trim($count[$k]);
					$item['total'] = round($total[$k],2);
					$item['surcharge'] = trim($surcharge[$k]);
					$array['item'][] = $item;
					$item_count ++;
					$item_total += trim($total[$k]);
				}
			}
			$array['count'] = $item_count;
			$array['total'] = round($item_total,2);
		}
		//return serialize($array);
		return $array;
	}
}


//$cart = new Cart();
//$cart->updateItem(array('2-10'));				//序号为2的商品为10个//
//$cart->addItem('2','bb','2000','10');			
//$cart->delCart();
//echo $cart->getTotal();
//echo " cnt ".$cart->getCount();
//$cart_array = $cart->getCart();

?>